package com.js.rtu.fragment.second;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.js.rtu.R;
import com.js.rtu.base.BaseFragment;
import com.js.rtu.config.CmdConfig;
import com.js.rtu.config.IniConfig;
import com.js.rtu.util.BluetoothUtils;
import com.js.rtu.util.CmdUtils;
import com.js.rtu.util.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 22_03设置终端工作参数
 * 定时自报间隔（单位为分）
 * 1 byte
 * 定时存储间隔的整数倍,默认配置在线型为 5 分钟,低功耗型为 60 分钟.
 * 时间<=60 时，设置命令里的时间等于实际自报时间
 * 时间=61 时，代表 60+1*60=120 分钟
 * 时间=62 时，代表 60+2*60=180 分钟
 * 时间=63 时，代表 60+3*60=240 分钟
 * 以此类推，即 60+(设定值-60)*60 分钟
 * Created by zwz on 2020/6/22.
 */
public class Cmd_22_03Fragment extends BaseFragment {


    @BindView(R.id.sp_list)
    Spinner spList;



    public static Cmd_22_03Fragment newInstance() {
        Cmd_22_03Fragment fragment = new Cmd_22_03Fragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_second_cmd_22_03;
    }

    @Override
    protected void initView() {

        ArrayAdapter<String> mAdapter = new ArrayAdapter<>(_mActivity, android.R.layout.simple_spinner_dropdown_item, IniConfig.zibaojiange_des);
        spList.setAdapter(mAdapter);
        for (int i = 0; i < IniConfig.zibaojiange_des.size(); i++) {
            if (IniConfig.zibaojiange_des.get(i).equals("1小时")) {
                    spList.setSelection(i);
                    break;
            }
        }



    }





    private void set() {
        if (IniConfig.zibaojiange_data.size()!=IniConfig.zibaojiange_des.size()){
            toastShow("ini配置文件有误,存储自报间隔节点中时间值与描述个数不一致");
            return;
        }
        String data= IniConfig.zibaojiange_data.get(spList.getSelectedItemPosition());
        data = String.format("%02x", Integer.parseInt(data));
        String msg = CmdConfig.CMD_22_03 + " " + data;
        sendData(msg);
    }

    private void sendData(String data) {
        String cmd = CmdUtils.getCmd(CmdConfig.CMD_22, data);
        BluetoothUtils.send(cmd, new BluetoothUtils.onCallback() {
            @Override
            public void onReceive(String msg) {
                dismissLoadingDialog();
                String[] data = msg.split(" ");
                if (data.length < 18) {
                    ToastUtil.info(msg);
                    return;
                }
                if (data[12].equals(CmdConfig.CMD_22)
                        && data[13].equals(CmdConfig.CMD_22_03)) {
                    if (data[14].equals(CmdConfig.SUCCECC)) {
                        ToastUtil.success("设置成功");
                    } else {
                        ToastUtil.error("设置失败");
                    }

                }


            }

            @Override
            public void onFail(String err) {
                toastShow(err);
                dismissLoadingDialog();
            }
        });
    }

    @OnClick(R.id.btn_set)
    public void onViewClicked() {
        set();
    }
}
